﻿using WebApp.Common;
using WebApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using WebApp.ViewModels.Manage;

namespace WebApp.Web.Core
{
    public class SessionManage
    {

        private const string Session_USER_KEY = "WebApp.User";
        private const string Session_USERROLE_KEY = "WebApp.User.Role";
        private const string Session_USER_ID = "userid";
        private const string Session_USER_NAME = "username";
        private const string Session_CLIENT_ID = "clientid";
        private const string Session_USER_LANGUAGE = "user.language";

        public static SessionUserModel User
        {
            get {
                var user = HttpContext.Current.Session[Session_USER_KEY] as SessionUserModel;
                return user;
            }
            set {
                HttpContext.Current.Session[Session_USER_KEY] = value;
                HttpContext.Current.Session[Session_USER_ID] = (value == null ? 0 : value.Id);
                HttpContext.Current.Session[WebApp.DAL.ContextFactory.Session_USER_ID] = (value == null ? 0 : value.Id);
                HttpContext.Current.Session[WebApp.DAL.ContextFactory.Session_USER_NAME] = (value == null ? "" : value.Username);
            }
        }

        public static List<string> UserRoles
        {
            get
            {
                return HttpContext.Current.Session[Session_USERROLE_KEY] as List<string>;
            }
            set
            {
                HttpContext.Current.Session[Session_USERROLE_KEY] = value;
            }
        }

        //public static string ClientID
        //{
        //    get
        //    {
        //        return Utils.NvStr(HttpContext.Current.Session[Session_CLIENT_ID]);
        //    }
        //    set
        //    {
        //        HttpContext.Current.Session[Session_CLIENT_ID] = value;
        //    }
        //}

        public static string Language
        {
            get
            {
                return Utils.NvStr(HttpContext.Current.Session[Session_USER_LANGUAGE]);
            }
            set
            {
                HttpContext.Current.Session[Session_USER_LANGUAGE] = value;
            }
        }
    }
}
